package com.wd.live.module_base.ui.base

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

abstract class BaseFragment<VM: ViewModel,DB: ViewDataBinding> : Fragment() {
    protected lateinit var dataBinding:DB;
    protected lateinit var viewModel: VM
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 创建 ViewModel 实例
        viewModel = ViewModelProvider(this)[getViewModelClass()]
        initData()
    }

    abstract fun initData()

    abstract fun getViewModelClass(): Class<VM>

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        dataBinding = DataBindingUtil.inflate(inflater,getLayout(), container, false)
        dataBinding?.lifecycleOwner = this
        return dataBinding?.root
    }

    abstract fun getLayout(): Int


}